﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace RFIDStockManagement.Common.Public
{
    /// <summary>
    /// 用于通常操作
    /// </summary>
    public static class PublicFunction
    {
        /// <summary>
        /// 用于取int性数据库数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int NotNullInt(object obj)
        {
            int retValue;
            retValue = (obj is DBNull) ? 0 : Convert.ToInt32(obj);
            return retValue;
        }

        /// <summary>
        /// String
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string NotNull(object obj)
        {
            string retValue;
            retValue = (obj is DBNull) ? string.Empty: obj.ToString();
            return retValue;
        }

        /// <summary>
        /// 用于取double型数据库数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static double NotNullDbl(object obj)
        {
            double retValue;
            retValue = (obj is DBNull) ? 0 : Convert.ToDouble(obj);
            return retValue;
        }

        /// <summary>
        ///  用于取long型数据库数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static long NotNullLng(object obj)
        {
            long retValue;
            retValue = (obj is DBNull) ? 0 : Convert.ToInt64(obj);
            return retValue;
        }

        /// <summary>
        /// Float,Single
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Single NotNullSng(object obj)
        {
            Single retValue;
            retValue = (obj is DBNull) ? 0 : Convert.ToSingle(obj);
            return retValue;
        }

        /// <summary>
        /// 时间类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime NotNullDateTime(object obj)
        {
            DateTime retValue;
            retValue = (obj is DBNull) ? DateTime.MinValue : Convert.ToDateTime(obj);
            return retValue;
        }


        /// <summary>
        /// 返回一个'值'内容
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static string AddToString(object a)
        {
            return "'" + a.ToString() + "'";
        }

        /// <summary>
        /// 返回一个'值'+逗号
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static string AddToStringWithCom(object a)
        {
            return "'" + a.ToString() + "',";
        }

        /// <summary>
        /// 返回一个值+逗号
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static string AddToValueWithCom(object a)
        {
            return a.ToString() + ",";
        }


        // 使用MD5加密encrypt
        /// </summary>
        /// <param name="str">需要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string GetMd5(string str)
        {
            StringBuilder md5Str = new StringBuilder();
            MD5 md5 = MD5.Create();  //实例化一个md5对像
            byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(str));//加密后是一个字节类型的数组
            //将得到的字符串使用十六进制类型格式   
            for (int i = 0; i < bytes.Length; i++)
            {
                md5Str.Append(bytes[i].ToString());
            }
            return md5Str.ToString();
        }

        public static void WriteFile(string path, string data)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            FileStream stream = new FileStream(path, FileMode.Create);
            byte[] arrData = new UTF8Encoding().GetBytes(data);
            stream.Write(arrData, 0, arrData.Length);
            stream.Flush();
            stream.Close();
        }
    }
}
